/*
 * LocalNode.cpp
 *
 *  Created on: Nov 21, 2014
 *      Author: toti
 */


#include <Network/LocalNode.hpp>


LocalNode::LocalNode(TCPConnection *tcpConnection, LocalNodeType localNodeType){

	this->localNodeType = localNodeType;
	this->tcpConnection = tcpConnection;

	rawDataLogOutputStream = new std::ofstream(Config::instance()->getRawDataLogFile());

}

bool LocalNode::handleInputData(std::list<MessagePackage*> *messageList){

	char rawInput;
	int len;


	while((len = tcpConnection->receiveData(&rawInput, sizeof(char))) > 0){

		inputStr.append(&rawInput,1);

		if(rawInput == 0 && inputStr.length() > 0){

			messageList->push_back(buildPackage(inputStr));

			*rawDataLogOutputStream << "Recv: " << inputStr << std::endl;

			inputStr.assign("");
		}


	}

	rawDataLogOutputStream->flush();

	if(len == 0){
		return true;
	}else{
		return false;
	}

}

void LocalNode::handleOutputData(MessagePackage *messagePackage){

	messagePackage->generateMessageString();
	tcpConnection->sendData((void*)messagePackage->getMessageString().c_str(),messagePackage->getMessageString().length()+1);

}


TCPConnection *LocalNode::getTcpConnection(){
	return tcpConnection;
}

LocalNode::LocalNodeType LocalNode::getLocalNodeType(){
	return localNodeType;
}
